package com.ruoyi.web.init;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.tranShort.domain.ShortMqttConfig;
import com.ruoyi.tranShort.domain.ShortSerialPortConfig;
import com.ruoyi.tranShort.service.IShortMqttConfigService;
import com.ruoyi.tranShort.service.IShortSerialPortConfigService;
import com.ruoyi.tranShort.service.impl.ShortSchedulingTaskServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @author wwy
 * @date 2021-08-29 18:56
 */
@Component
public class StartInit implements ApplicationRunner {
    private static final Logger LOGGER = LoggerFactory.getLogger(StartInit.class);
    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private IShortMqttConfigService shortMqttConfigService;
    @Autowired
    private IShortSerialPortConfigService shortSerialPortConfigService;
    @Override
    public void run(ApplicationArguments args) throws Exception {

        //mqtt连接
        //获取连接参数，判断是否需要连接
        String mqttConfigFlagStr = sysConfigService.selectConfigByKey("short.mqtt.auto.connect");
        boolean mqttConnectFlag = false;
        if("true".equals(mqttConfigFlagStr)){
            mqttConnectFlag = true;
        }
        if(mqttConnectFlag){
            LOGGER.info("=================开启自动连接mqtt=================");
            ShortMqttConfig shortMqttConfig = shortMqttConfigService.selectShortMqttConfig();
            if(shortMqttConfig != null){
                //校验参数
                if(StringUtils.isNotEmpty(shortMqttConfig.getIp())
                        && StringUtils.isNotEmpty(shortMqttConfig.getUsername())
                        &&StringUtils.isNotEmpty(shortMqttConfig.getPassword())
                        && shortMqttConfig.getPort()!=null){
                    //可以进行连接
                    int connect = shortMqttConfigService.connect(shortMqttConfig);
                }
            }
        }
        //串口自动连接
        String serialPortConfigFlagStr = sysConfigService.selectConfigByKey("short.serialPort.auto.connect");
        boolean serialPortConnectFlag = false;
        if("true".equals(serialPortConfigFlagStr)){
            serialPortConnectFlag = true;
        }

        if(serialPortConnectFlag){
            LOGGER.info("=================开启自动连接串口=================");
            ShortSerialPortConfig shortSerialPortConfig = shortSerialPortConfigService.selectShortSerialPortConfig();
            if(shortSerialPortConfig !=null){
                //参数校验
                if(StringUtils.isNotEmpty(shortSerialPortConfig.getPort())){
                    int connect = shortSerialPortConfigService.connect(shortSerialPortConfig);
                }

            }
        }



        LOGGER.info("================设置自动叫哨提前时间==================");
        //设置叫哨提前时间
        String leadTimeStr = sysConfigService.selectConfigByKey("short.autoSendAdvanceTime");
        try {
            int leadTime = Integer.parseInt(leadTimeStr);
            ShortSchedulingTaskServiceImpl.leadTime = leadTime;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            LOGGER.info("================设置自动叫哨提前时间异常==================");
        }
        LOGGER.info("================设置自动叫哨提前时间为{}分钟==================",leadTimeStr);
    }
}
